package com.sina.weibo.sdk.network.base;

import android.os.Bundle;
import com.huawei.hms.framework.common.ContainerUtils;
import com.sina.weibo.sdk.network.IRequestParam;
import com.sina.weibo.sdk.network.IRequestParam.ValuePart;
import com.sina.weibo.sdk.network.impl.RequestParam;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

public class RequestBodyHelper {
    private static final String DEFAULT_CHARSET = "UTF-8";
    private static final String OCTET_STREAM = "application/octet-stream";

    public static void fillRequestBody(IRequestParam iRequestParam, HttpURLConnection httpURLConnection, String str) throws IOException {
        byte[] byteArray = iRequestParam.getPostBundle().getByteArray(RequestParam.KEY_PARAM_BODY_BYTE_ARRAY);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        OutputStream dataOutputStream = new DataOutputStream(outputStream);
        if (byteArray != null) {
            if (iRequestParam.needGzip()) {
                dataOutputStream.write(transGzip(byteArray));
            } else {
                dataOutputStream.write(byteArray);
            }
        } else if (isMultipartRequest(iRequestParam)) {
            fillMultiBody(dataOutputStream, iRequestParam, str);
        } else {
            Bundle postBundle = iRequestParam.getPostBundle();
            StringBuilder stringBuilder = new StringBuilder();
            Object obj = 1;
            for (String str2 : postBundle.keySet()) {
                if (obj != null) {
                    obj = null;
                } else {
                    stringBuilder.append(ContainerUtils.FIELD_DELIMITER);
                }
                try {
                    stringBuilder.append(URLEncoder.encode(str2, DEFAULT_CHARSET)).append(ContainerUtils.KEY_VALUE_DELIMITER).append(URLEncoder.encode(String.valueOf(postBundle.get(str2)), DEFAULT_CHARSET));
                } catch (Exception e) {
                }
            }
            DataOutputStream dataOutputStream2 = new DataOutputStream(dataOutputStream);
            if (iRequestParam.needGzip()) {
                dataOutputStream2.write(transGzip(stringBuilder.toString().getBytes(DEFAULT_CHARSET)));
            } else {
                dataOutputStream2.write(stringBuilder.toString().getBytes(DEFAULT_CHARSET));
            }
            dataOutputStream2.flush();
            dataOutputStream2.close();
        }
        outputStream.flush();
        outputStream.close();
    }

    public static boolean isMultipartRequest(IRequestParam iRequestParam) {
        return (iRequestParam.files().isEmpty() && iRequestParam.byteArrays().isEmpty()) ? false : true;
    }

    private static void fillMultiBody(OutputStream outputStream, IRequestParam iRequestParam, String str) throws IOException {
        Bundle postBundle = iRequestParam.getPostBundle();
        String str2 = "--";
        String str3 = "\r\n";
        String str4 = "\"UTF-8\"";
        for (String str5 : postBundle.keySet()) {
            StringBuilder stringBuilder = new StringBuilder(100);
            stringBuilder.setLength(0);
            stringBuilder.append(str2);
            stringBuilder.append(str).append(str3);
            stringBuilder.append("Content-Disposition: form-data; name=\"").append(str5).append("\"").append(str3);
            stringBuilder.append("Content-Type: text/plain;charset:" + str4 + str3);
            stringBuilder.append("Content-Transfer-Encoding: 8bit").append(str3);
            stringBuilder.append(str3);
            stringBuilder.append(String.valueOf(postBundle.get(str5))).append(str3);
            outputStream.write(stringBuilder.toString().getBytes(DEFAULT_CHARSET));
        }
        Map files = iRequestParam.files();
        for (String str52 : files.keySet()) {
            ValuePart valuePart = (ValuePart) files.get(str52);
            File file = (File) valuePart.value;
            String str6 = valuePart.mimeType;
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(str2);
            stringBuffer.append(str);
            stringBuffer.append(str3);
            stringBuffer.append("Content-Disposition: form-data; name=\"").append(str52).append("\";").append("filename=\"").append(file.getName()).append("\"").append(str3);
            stringBuffer.append("Content-Type: " + str6 + ";charset:" + str4 + str3);
            stringBuffer.append(str3);
            outputStream.write(stringBuffer.toString().getBytes(DEFAULT_CHARSET));
            InputStream fileInputStream = new FileInputStream(file);
            byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
            while (true) {
                int read = fileInputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                outputStream.write(bArr, 0, read);
            }
            fileInputStream.close();
            outputStream.write(str3.getBytes());
        }
        Map byteArrays = iRequestParam.byteArrays();
        for (String str522 : byteArrays.keySet()) {
            StringBuffer stringBuffer2 = new StringBuffer();
            stringBuffer2.append(str2);
            stringBuffer2.append(str);
            stringBuffer2.append(str3);
            stringBuffer2.append("Content-Disposition: form-data; name=\"").append(str522).append("\"").append(str3);
            stringBuffer2.append("Content-Type: text/plain;charset:\"UTF-8\"" + str3);
            stringBuffer2.append(str3);
            outputStream.write(stringBuffer2.toString().getBytes());
            outputStream.write((byte[]) byteArrays.get(str522));
            outputStream.write(str3.getBytes());
        }
        outputStream.write((str2 + str + "--" + str3).getBytes());
        outputStream.flush();
        outputStream.close();
    }

    private static byte[] transGzip(byte[] bArr) {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
            gZIPOutputStream.write(bArr);
            gZIPOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return byteArrayOutputStream.toByteArray();
    }

    public static String getBoundry() {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i < 12; i++) {
            long currentTimeMillis = System.currentTimeMillis() + ((long) i);
            if (currentTimeMillis % 3 == 0) {
                stringBuffer.append(((char) ((int) currentTimeMillis)) % 9);
            } else if (currentTimeMillis % 3 == 1) {
                stringBuffer.append((char) ((int) ((currentTimeMillis % 26) + 65)));
            } else {
                stringBuffer.append((char) ((int) ((currentTimeMillis % 26) + 97)));
            }
        }
        return stringBuffer.toString();
    }
}
